package com.crm.system.service.impl.contactManager;

import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.contactManager.Contact;
import com.crm.model.entity.contactManager.ContactDetail;
import com.crm.model.entity.contactManager.ContactInformation;
import com.crm.model.entity.customerManager.Customer;
import com.crm.model.entity.customerManager.CustomerContactPerson;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.contactManager.ContactInformationVO;
import com.crm.model.vo.customerManager.CustomerContactVO;
import com.crm.service.contactManager.ContactDbService;
import com.crm.service.contactManager.ContactDetailDbService;
import com.crm.service.contactManager.ContactInformationDbService;
import com.crm.service.customerManager.CustomerContactPersonDbService;
import com.crm.service.customerManager.CustomerDbService;
import com.crm.system.service.contactManager.ContactDetailService;
import com.crm.system.service.contactManager.ContactInformationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * bj
 * 联系人管理信息
 */
@Service
public class ContactInformationServiceImpl implements ContactInformationService {

    @Autowired
    private ContactDbService contactDbService;

    @Autowired
    private ContactInformationDbService contactInformationDbService;

    @Autowired
    private CustomerContactPersonDbService customerContactPersonDbService;

    @Autowired
    private CustomerDbService customerDbService;

    /**
     * 联系人-新增联系方式
     * @param contactInformation 联系方式实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public Boolean addContactInformation(ContactInformationVO contactInformation) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();

        //查询是否是主联系人
        if(contactInformation.getContactInformationType() != null){
            if(contactInformation.getContactInformationType() == 0) {
                //通过联系人id和主联系方式查询出是主联系方式的那一条记录
                List<ContactInformation> list = contactInformationDbService.getContactIdAndContactInformationType(contactInformation.getContactId(),contactInformation.getContactInformationType());
                if(list != null && list.size() > 0){
                    for (int i =0; i<list.size(); i++) {
                        ContactInformation contactInfo = list.get(i);
                        contactInfo.setContactInformationType(1);
                        contactInfo.setUpdatePeopleId(userAccount.getAccountId());
                        contactInfo.setUpdateTm(new Date());
                        contactInformationDbService.updateContactInformation(contactInfo);

                        if(i == 0){
                            //通过联系人id查询联系人信息来修改联系人的主联系方式
                            Contact contact = contactDbService.selectContactById(contactInfo.getContactId());
                            contact.setContactInformation(contactInformation.getContactInformation());
                            contact.setUpdatePeopleId(userAccount.getAccountId());
                            contact.setUpdateTm(new Date());
                            contactDbService.updateContact(contact);

                            //通过客户id和联系人名称名称和联系方式来修改客户下联系人的联系方式成为主联系人

                            if(contact.getContactNature() == 0) {
                                CustomerContactPerson customerContactPerson = customerContactPersonDbService.getCustomerIdAndContactNameAndLxfs(contact.getCustomerId(),contact.getContactName(),contactInfo.getContactInformation());
                                customerContactPerson.setContactInformation(contactInformation.getContactInformation());
                                customerContactPerson.setUpdatePeopleId(userAccount.getAccountId());
                                customerContactPerson.setUpdateTm(new Date());
                                //修改客户下联系人表
                                customerContactPersonDbService.updateCustomerContactPerson(customerContactPerson);
                            }else{
                                Customer customer = customerDbService.selectCustomerById(contact.getCustomerId());
                                customer.setContactPersonPhone(contactInformation.getContactInformation());
                                customer.setUpdatePeopleId(userAccount.getAccountId());
                                customer.setUpdateTm(new Date());
                                customerDbService.updateCustomerByPrimaryKey(customer);
                            }
                        }



                    }
                }

            }
        }

        String contactInformationId = CommonUtils.getGUID();
        ContactInformation info = new ContactInformation();
        info.setContactInformationId(contactInformationId);
        info.setContactInformation(contactInformation.getContactInformation());
        info.setContactType(contactInformation.getContactType());
        info.setContactInformationType(contactInformation.getContactInformationType());
        info.setCreatePeopleId(userAccount.getAccountId());
        info.setCreateTm(new Date());
        info.setUpdatePeopleId(userAccount.getAccountId());
        info.setUpdateTm(new Date());
        info.setAbolishState(1);  //废止状态  1启动 2废止
        info.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        info.setCustomerId(contactInformation.getCustomerId());
        info.setContactId(contactInformation.getContactId());
        info.setCompanyId(userAccount.getCompanyId());

        //新增联系方式
        Boolean result = contactInformationDbService.addContactInformation(info);
        return result;
    }

    /**
     * 联系人-新增联系方式-联系方式重复
     * @param contactId  联系人id
     * @param contactInformation 联系方式
     * @return
     */
    @Override
    public ContactInformation getContactIdAndContactInformation(String contactId, String contactInformation) {
        return contactInformationDbService.getContactIdAndContactInformation(contactId,contactInformation);
    }

    /**
     * 根据联系人id查询联系人下的联系方式
     * @param pageIndex 页码
     * @param pageSize 分页大小
     * @param sortName 排序列名字
     * @param sortOrder 排序(asc/desc)
     * @param contactId 联系人id
     * @return
     */
    @Override
    public DhPageInfo<ContactInformation> contactInformationList(Integer pageIndex, Integer pageSize, String sortName, String sortOrder, String contactId) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }

        String order = "abolish_state asc,contact_information_type,create_tm desc";
        List<ContactInformation> list = contactInformationDbService.contactInformationList(order,contactId);


        PageInfo<ContactInformation> pageInfo = new PageInfo<ContactInformation>(list);
        return new DhPageInfo<ContactInformation>(pageInfo, list);
    }

    /**
     * 呼叫中心根据手机号和账号Id查询联系人
     * @Author pym
     * @Date  2021/7/1 18:14
     * @param contactInformation 联系方式
     * @param accountId 账号Id
     * @return {@link {com.crm.model.vo.contactManager.ContactInformationVO}}
     */
    @Override
    public ContactInformationVO getContactInformationByContactInfoAndId(String contactInformation, String accountId) {
        return contactInformationDbService.getContactInformationByContactInfoAndId(contactInformation,accountId);
    }
}
